package com.sjz.bookmark.tree;

import com.intellij.openapi.ui.SimpleToolWindowPanel;
import com.intellij.ui.components.JBScrollPane;
import com.intellij.ui.treeStructure.Tree;

import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;

public class MyToolWindow extends SimpleToolWindowPanel {

    private Tree tree;

    public MyToolWindow() {
        super(true, true);

        // 生成树数据
        DefaultMutableTreeNode root = TreeDataGenerator.generateTree();
        DefaultTreeModel model = new DefaultTreeModel(root);

        // 创建树
        tree = new Tree(model);
        tree.setDragEnabled(true);
        tree.setDropMode(DropMode.ON_OR_INSERT);

        // 设置 TransferHandler
        tree.setTransferHandler(new TreeTransferHandler(tree));

        // 添加滚动条
        JBScrollPane scrollPane = new JBScrollPane(tree);
        setContent(scrollPane);
    }

    public void addItem(MyObject item) {
        DefaultMutableTreeNode root = (DefaultMutableTreeNode) tree.getModel().getRoot();
        DefaultMutableTreeNode node = new DefaultMutableTreeNode(item);
        root.add(node);
        ((DefaultTreeModel) tree.getModel()).reload();
    }
}